package com.liachen.common.util;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.liachen.common.constant.Constant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Comparator;
import java.util.Date;
import java.util.List;

import static java.util.stream.Collectors.*;
@Slf4j
public class StockUtil {
    public static void main(String[] args) {
        /**
         * 1、https://stock.cheesefortune.com/stock?stockCode=300726.SZ
         * 2、估值与分位
         * 3、https://stock.cheesefortune.com/api/v3/details/pepb?code=300726.SZ&type=pe&years=10Y&tryHard=true
         * 4、返回的json复制到pe.txt
         *
         * 5、成长能力 归母净利润
         * 6、https://stock.cheesefortune.com/api/v3/details/growAbility?code=600519.SH&dataType=single&type=2&years=10
         * 7、
         */
        String content = FileUtil.readString("pe-爱尔眼科-20240601.txt", "UTF-8");
        log.info(content);
        JSONArray jsonArray = JSONUtil.parseObj(content).getJSONObject("datas").getJSONArray("datas");
        Date now = DateUtil.date();
        String filterDateStr = DateUtil.offset(now, DateField.YEAR, -10).toString(DatePattern.PURE_DATE_PATTERN);
        List<Double> peList = jsonArray.stream().map(JSONUtil::parseObj).filter(data -> data.getLong("x") > Long.parseLong(filterDateStr)).map(data -> data.getDouble("d")).collect(toList());
        Double middenPe = CollectionUtil.getMedian(peList);

        String growContent = FileUtil.readString("grow-爱尔眼科-20240601.txt", "UTF-8");
        log.info(growContent);
        JSONArray growArray = JSONUtil.parseObj(growContent).getJSONObject("datas").getJSONArray("datas");
        List<NetProfit> growList = JSONUtil.toList(growArray, NetProfit.class);
        Long netProfit = getNetProfit(growList);

        Double price = netProfit * middenPe;

        Double firstBuy = price * 0.65;
        Double secondBuy = price * 0.5;

        Double firstSell = price * 1.5;
        Double secondSell = price * 2;
        Double thirdSell = price * 2.5;

        log.info("middenPe:{}", middenPe);
        log.info("netProfit:{}", netProfit);
        log.info("合理估值：{}", price.longValue());

        log.info("第一买点：{}", firstBuy.longValue());
        log.info("第二买点：{}", secondBuy.longValue());

        log.info("第一卖点：{}", firstSell.longValue());
        log.info("第二卖点：{}", secondSell.longValue());
        log.info("第一卖点：{}", thirdSell.longValue());
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class NetProfit {
        private String reportPeriod;
        private Long value;
    }

    private static Long getNetProfit(List<NetProfit> netProfitList) {
        netProfitList = netProfitList.stream().sorted(Comparator.comparing(NetProfit::getReportPeriod).reversed()).collect(toList());
        Long totalGrow = 0L;
        for (int i=0; i < 4; i++) {
            totalGrow += netProfitList.get(i).getValue();
        }
        return totalGrow;
    }

    public static String getStockBasePath() {
        DateTime now = DateUtil.date();
        DateTime endOfQuarter = DateUtil.endOfQuarter(DateUtil.offset(now, DateField.DAY_OF_YEAR, -90));
        String secondPath =  DateUtil.formatDate(endOfQuarter);
        return String.format("%s\\%s", Constant.STOCK_FILE_PATH, secondPath);
    }
}
